package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    SetMealMapper setMealMapper;

    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    @Transactional
    public void insert(SetmealVO setmealVO) {
        //新增套餐表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setMealMapper.insert(setmeal);
        //新增套餐-菜品关系表
        List<SetmealDish> setmealDish=setmealVO.getSetmealDishes();
        for (SetmealDish value: setmealDish){
            value.setSetmealId(setmeal.getId());
        }
        System.out.println(setmealDish.size());
        setMealDishMapper.insert(setmealDish);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setMealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> setmealVOS=page.getResult();
        for(SetmealVO value:setmealVOS){
            String name = categoryMapper.getNameById(value.getCategoryId());
            value.setCategoryName(name);
        }
        return new PageResult(total,setmealVOS);
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        log.info("修改套餐：{}",setmealDTO);
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDish=setmealDTO.getSetmealDishes();
        Long setmealId=setmeal.getId();
        setMealMapper.update(setmeal);
        setMealDishMapper.deleteBySetmealId(setmealId);
        for (SetmealDish value : setmealDish){
            value.setSetmealId(setmealId);
        }
        setMealDishMapper.insert(setmealDish);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal=setMealMapper.getById(id);
        List<SetmealDish> setmealDish=setMealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        log.info(".......:{}",setmealDish);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    @Override
    public void setStatus(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.update(setmeal);

    }

    @Override
    public void delete(List<Long> ids) {
        setMealMapper.delete(ids);
        for(Long value:ids){
            setMealDishMapper.deleteBySetmealId(value);
        }

    }

    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {
        List<Setmeal> list=setMealMapper.getByCategoryId(categoryId);
        return list;
    }

    @Override
    public List<DishItemVO> getBySetmealId(Long id) {
        List<SetmealDish> list=setMealDishMapper.getBySetmealId(id);
        List<DishItemVO> dishItemVOS=new ArrayList<>();
        for(SetmealDish value:list){
            DishItemVO dishItemVO=new DishItemVO();
            BeanUtils.copyProperties(value,dishItemVO);
            dishItemVOS.add(dishItemVO);
        }
        for(DishItemVO value : dishItemVOS){
            String image=dishMapper.getImageByName(value.getName());
            value.setImage(image);
            String description=dishMapper.getDescriptionByName(value.getName());
            value.setDescription(description);
            if(value.getDescription()==null){
                value.setDescription("菜品无描述");
            }
        }
        return dishItemVOS;
    }
}
